<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
     
    /**
     * 真正的验证
     * @param array $params 需要验证的参数
     * @param array $rules  验证的规则数组
     * @return string|Ambigous 
     */
    public function validform($params, $rules) {
    	
    	// Do we even have any data to process?  Mm?
        if (count($params) == 0)
        {
        	 return "";
        }
        // Does the _field_data array containing the validation rules exist?
        if (count($rules) == 0)
        {
           return "";
        }
        //用来验证
        $_params=array();
        //修改params
        foreach($params as $k=>$v){
        	if(!$v) unset($params[$k]);
        	else array_push($_params, $k);
        }
        
       // print_r($_params); die();
       //循环必须字段 是否在 发送过来的post里
      
        foreach ($rules as $field => $row)
        {
           
        	//验证 参数是否在$params里面
            if(in_array($row['field'], $_params)){
            	
            	// array ('field' => 'username','label' => '用户名','rules' => 'required',),   require , username
               $error=$this->execute($row, explode('|', $row['rules']),$params[$row['field']]);
               if($error!=""){
                 return $error;
               }
               // break;
              
            }else{
            	
            	$error=$row['field']."的值不可以为空";
            	
            	//break;
            	
            	return $error;
            }
        }
    }
     
    /**
     * 真正的验证
     */
    private function execute($row, $rules, $postdata = NULL, $cycles = 0){
        
        /**
         * 循环验证规则文件
         */
    //Array ( [field] => username [label] => 用户名 [rules] => required ) Array ( [0] => required ) aaa
    
        foreach ($rules As $rule)
        {
//           if ($rule=='email' ){
//           	  $aaa=$this->valid_email($postdata);
//           	  if(!$aaa) echo(555555);
//           }
        	if ($rule=='required' && is_null($postdata)){$error=$row['label']."不可以为空！";break;}
        
            if ($rule=='email' AND !$this->valid_email($postdata)){$error=$row['label']."不合乎Email规范！";break;}
            if ($rule=='integer' && !$this->integer($postdata)){$error=$row['label']."必须为整数！";break;}
            if ($rule=='numberic_no_zero' && !$this->is_natural_no_zero($postdata)){$error=$row['label']."必须为非零的正整数！";break;}
            if ($rule=='numberic' && !$this->is_numeric($postdata)){$error=$row['label']."必须为数字！";break;}
            //验证最大或者最小
            $callback = FALSE;
            if (substr($rule, 0, 9) == 'callback_')
            {
                $rule = substr($rule, 9);
                $callback = TRUE;
            }
            $param = false;
            if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
            {
                $rule   = $match[1];
                $param  = $match[2];
            }
            if ($callback === TRUE){
                $error=$this->valid_function($row,$rule,$postdata,$param);
            }else{
                $error=$this->valid_function($row,$rule,$postdata,$param);
            }
             
        }
        return $error;
    }
     
    private function valid_function($row,$rule,$postdata){
        $rule = substr($rule, 9);
        $param = FALSE;
        if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
        {
            $rule   = $match[1];
            $param  = $match[2];
        }
        //如果不存在方法
        if (! method_exists($this->CI, $rule))return "";
        // Run the function and grab the result
        $result = $this->CI->$rule($postdata, $param);//调用类的方法
        return $this->result_format($row,$result);
    }
     
    private function result_format($row,$result){
        if(!$result)return $row['label']."验证失败！";
        return "";
    }
}